/** 
 * ColorSelectorButton.java
 * @author Lex
 * 2007-9-8 ����01:15:35
 */
package org.lex.swing.widgets;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import org.lex.io.IO;
import org.lex.swing.SelectAllWhenGainFocus;
import org.lex.swing.Swings;
import org.lex.utils.SystemProperties;


public class DirectorySelector extends JPanel {
	public static final String SELECTED_DIRECTORY_CHANGED_PROPERTY = "selectedDirectoryChangedProperty";
	private JTextField textField;
	private JButton selectButton;
	// ==========================
	private File directory = null;

	public static void main(String[] args) {
		Swings.show(new DirectorySelector());
	}

	// =========================
	// Constructors
	// =========================
	public DirectorySelector() {
		this(null);
	}

	public DirectorySelector(File directory) {
		super();
		this.initialize();
		if (null == directory)
			directory = new File(SystemProperties.userHome());
		this.setDirectory(directory, false);
	}

	public File getDirectory() {
		return directory;
	}

	public void setDirectory(File directory) {
		this.setDirectory(directory, true);
	}

	protected void setDirectory(File directory, boolean fireEvent) {
		File old = this.directory;
		this.directory = directory;
		this.getTextField().setText(IO.getCanonicalPath(directory));
		if (true == fireEvent)
			super.firePropertyChange(SELECTED_DIRECTORY_CHANGED_PROPERTY, old, directory);
	}

	public void addDirectoryChangeListener(PropertyChangeListener l) {
		super.addPropertyChangeListener(SELECTED_DIRECTORY_CHANGED_PROPERTY, l);
	}

	public void removeDirectoryChangeListener(PropertyChangeListener l) {
		super.removePropertyChangeListener(SELECTED_DIRECTORY_CHANGED_PROPERTY, l);
	}

	private void initialize() {
		this.setLayout(new GridBagLayout());
		this.add(getTextField(), new GridBagConstraints(0, 0, 1, 1, 10.0, 0.0, GridBagConstraints.BASELINE, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));
		this.add(getSelectButton(), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BASELINE, GridBagConstraints.NONE, new Insets(0,
				5, 0, 0), 0, 0));
	}

	private JButton getSelectButton() {
		if (selectButton == null) {
			selectButton = new JButton();
			selectButton.setText("...");
			selectButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					new SwingWorker<Void, Void>() {
						protected Void doInBackground() throws Exception {
							select();
							return null;
						}
					}.execute();
				}

				private void select() {
					JFileChooser fc = new JFileChooser(directory);
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					fc.setMultiSelectionEnabled(false);
					int opt = fc.showOpenDialog(DirectorySelector.this);
					if (JFileChooser.APPROVE_OPTION == opt) {
						File dir = fc.getSelectedFile();
						if (false == dir.exists() || false == dir.isDirectory())
							dir.mkdirs();
						setDirectory(dir);
					}
				}
			});
		}
		return selectButton;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.addFocusListener(SelectAllWhenGainFocus.getInstance());
		}
		return textField;
	}
}
